/*
 * harry_pagination: a jQuery plugin, version: 1.5.0 beta (2012-3-13)
 * @requires jQuery v1.2.3 or later
 *
 * For usage and examples, visit:
 * http://crazyhr.com/
 * Licensed under the MIT:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Copyright (c) 2012, Harry He
 */
(function($){
    $.fn.harrypag = function(options){
        var defaults = {
            url:"product_relatedrpt_data_cat.do",
			data:{pageSize: 10, pageNo: 1},
			success:function(){
			},
			searchBtn:null,
			searchParam:{},
			iniSearch: true,
			ppIcon: []
        },_this=this;
		this.HPAG = $.extend(defaults, options);
        var $t=$(this);
        if(this.HPAG.data.pageNo === null || this.HPAG.data.pageNo < 1){
        	this.HPAG.data.pageNo = 1;
        }
        this.HPAG.currPage = this.HPAG.data.pageNo;
        if(this.HPAG.data.pageSize === null || this.HPAG.data.pageSize < 1){
        	this.HPAG.data.pageSize = 10;
        }
        
        //getData
		this.getData=function(){
			var param = _this.HPAG.searchParam;
			for(var item in param){
				eval('_this.HPAG.data.'+item+' = param[item].val()');
			}
			_this.HPAG.data.ramdom = (new Date).getTime();
			$.getJSON(this.HPAG.url, this.HPAG.data , function (json) {
				_this.HPAG.currPage = _this.HPAG.data.pageNo;
				var pageCount = json.pageCount;
				_this.HPAG.pageCount = pageCount;
		        var dataList = json.dataList;
		        _this.success(dataList);
				_this.render(_this.HPAG.currPage,pageCount);
		     });
		};
		
		this.success=this.HPAG.success;
		
		this.render=function(currPage,numPages){
			var html="<table id='harrypag'><tr>";
			var hpg_ppI = _this.HPAG.ppIcon;
			if(hpg_ppI.length > 0){
				for(var i=0; i < hpg_ppI.length; i++){
					hpg_cI = hpg_ppI[i];
					if(hpg_cI>0){
						var t_cl = "class='grayicon'";
						if(_this.HPAG.data.pageSize == hpg_cI){t_cl="class='blueicon'"}
						html+="<td><a href='javascript:void(0)' "+t_cl+" id='hpg_ppI_"+hpg_cI+"' title='"+hpg_cI+"条/页'>"+hpg_cI+"</a></td>";
					}
				}
			}
			html += "<td><a href='javascript:void(0)' id='goto_one' title='首页'></a></td>";
			html += "<td><a href='javascript:void(0)' id='goto_l' title='上一页'></a></td>";
			html += "<td><input type='text' size='1' id='page_num' value='"+currPage+"'/></td><td>页/"+numPages+"页</td>";
			html += "<td><a href='javascript:void(0)' id='goto_r' title='下一页'></a></td>";
			html += "<td><a href='javascript:void(0)' id='goto_end' title='尾页'></a></td></tr></table>";
			$t.html(html);
		};
		
		this.gotoPage=function(page){
			var strP=/^\d+$/;
			if(strP.test(page)){
  				if(page < 1){
					_this.HPAG.data.pageNo = 1;
				} else if(page > eval(_this.HPAG.pageCount)){
					_this.HPAG.data.pageNo = _this.HPAG.pageCount;
				} else {
					_this.HPAG.data.pageNo = page;
				}
				_this.getData();
  			} else {
  				_this.HPAG.data.pageNo = 1;
  				_this.getData();
  			}
		};
		
		if(this.HPAG.iniSearch){
			_this.getData();
		}
		
		$t.find("#goto_one").die("click");
		$t.find("#goto_one").live("click",function(){
			_this.HPAG.data.pageNo = 1;
			_this.getData();
		});
		
		$t.find("#goto_end").die("click");
		$t.find("#goto_end").live("click",function(){
			_this.HPAG.data.pageNo = _this.HPAG.pageCount;
			_this.getData();
		});
		
		$t.find("#goto_l").die("click");
		$t.find("#goto_l").live("click",function(){
			if(_this.HPAG.data.pageNo > 1){
				_this.HPAG.data.pageNo = _this.HPAG.currPage - 1;
			} else {
				return false;
			}
			_this.getData();
		});
		
		$t.find("#goto_r").die("click");
		$t.find("#goto_r").live("click",function(){
			if(_this.HPAG.data.pageNo < _this.HPAG.pageCount){
				_this.HPAG.data.pageNo = _this.HPAG.currPage + 1;
			} else {
				return false;
			}
			_this.getData();
		});
		
		$t.find("#page_num").die("blur");
		$t.find("#page_num").live("blur",function(){
			_this.gotoPage($(this).val());
		});
		
		$t.find("#page_num").die("keyup");
		$t.find("#page_num").live("keyup",function(event){
			if(event.which == 13){
				_this.gotoPage($(this).val());
				$t.blur();
			} else{
				return false;
			}
		});
		
		if(_this.HPAG.searchBtn != null){
			_this.HPAG.searchBtn.live("click",function(){
				_this.HPAG.data.pageNo = 1;
	  			_this.getData();
			});
		}
		
		if(_this.HPAG.ppIcon.length > 0){
			for(var i=0; i < _this.HPAG.ppIcon.length; i++){
				hpg_cI = _this.HPAG.ppIcon[i];
				if(hpg_cI>0){
					var t_cl = "class='grayicon'";
					$t.find("#hpg_ppI_"+hpg_cI).die("click");
					$t.find("#hpg_ppI_"+hpg_cI).live("click",function(){
						_this.HPAG.data.pageSize = $(this).attr("id").substr(8);
						_this.HPAG.data.pageNo = 1;
						_this.getData();
					});
				}
			}
		}
		
		return this;
    };
})(jQuery);